package com.wyzz.hr.report.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.IAttendmonthlyBO;
import com.wyzz.hr.bo.ILeavetypeBO;
import com.wyzz.hr.bo.IOvertimetypeBo;
import com.wyzz.hr.pojo.Leaverequest;
import com.wyzz.hr.pojo.Overtimerequest;
import com.wyzz.hr.report.bo.IExaminReportBo;
import com.wyzz.hr.report.bo.IReportBo;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DateUtil;

public class ExaminReportAction extends BaseAction {

	public ExaminReportAction() {
		yearList = new ArrayList();
		attendmonthlyYearList = new ArrayList();
		designSelect = "";
	}

	public String execute() throws Exception {
		ILeavetypeBO lt_BO = (ILeavetypeBO) getBean("leavetypeBO");
		lrtypeList = lt_BO.FindAllLeaveType();
		IOvertimetypeBo ot_BO = (IOvertimetypeBo) getBean("overtimetypeBO");
		ottypeList = ot_BO.FindAllOtType();
		IReportBo r_BO = (IReportBo) getBean("reportBo");
		List tmp1 = r_BO.exeHqlList((new StringBuilder()).append(
				"select distinct min(year(").append(
				Leaverequest.PROP_LR_START_DATE).append(")) from ").append(
				Leaverequest.REF).toString());
		List tmp2 = r_BO.exeHqlList((new StringBuilder()).append(
				"select distinct min(year(").append(
				Overtimerequest.PROP_OR_START_DATE).append(")) from ").append(
				Overtimerequest.REF).toString());
		Integer endYear = Integer.valueOf(DateUtil.getYear(new Date()));
		Integer startYear = endYear;
		if (tmp1 != null && tmp1.size() > 0 && tmp1.get(0) != null) {
			Integer tmpYear = (Integer) tmp1.get(0);
			if (tmpYear.intValue() < startYear.intValue())
				startYear = tmpYear;
		}
		if (tmp2 != null && tmp2.size() > 0 && tmp2.get(0) != null) {
			Integer tmpYear = (Integer) tmp2.get(0);
			if (tmpYear.intValue() < startYear.intValue())
				startYear = tmpYear;
		}
		for (Integer i = startYear; i.intValue() <= endYear.intValue();) {
			yearList.add(i.toString());
			Integer integer = i;
			Integer integer1 = i = Integer.valueOf(i.intValue() + 1);
			Integer _tmp = integer;
		}

		IAttendmonthlyBO attendmothlyBO = (IAttendmonthlyBO) SpringBeanFactory
				.getBean("attendmonthlyBo");
		attendmonthlyYearList = attendmothlyBO.getAllYears();
		if (getRequest().getParameter("repeated") != null
				&& getRequest().getParameter("repeated").length() > 0) {
			designSelect = "repeat";
			return "success";
		}
		String reportID = getRequest().getParameter("reportSelect");
		if (reportID != null) {
			if (reportID.equals("report1"))
				designSelect = "lr";
			else if (reportID.equals("report2"))
				designSelect = "ot";
			else
				return "error";
		} else {
			startMonths = "m1";
			endMonths = "m12";
			year = (new StringBuilder()).append(Calendar.getInstance().get(1))
					.append("").toString();
			attendmonthlyYear = year;
			designSelect = "none";
		}
		return "success";
	}

	public String executeEmpLr() throws Exception {
		ServletRequest request = getRequest();
		if (getRequest().getParameter("_format") != null
				&& getRequest().getParameter("_format").equals("pdf")) {
			request.setAttribute("_format", "PDF");
			request.setAttribute("_contentType", "attachment");
			request.setAttribute("_fileName", "empLrReport");
		} else {
			request.setAttribute("_format", "HTML");
		}
		Map params = new HashMap();
		String year = getRequest().getParameter("year");
		String startmonths = getRequest().getParameter("startMonths")
				.substring(1);
		String endmonths = getRequest().getParameter("endMonths").substring(1);
		String type = getRequest().getParameter("examinType");
		String deptInclude = getRequest().getParameter("includeDeptPass");
		try {
			params.put("startTime", (new SimpleDateFormat("yyyy-MM-dd"))
					.format((new SimpleDateFormat("yyyy-MM-dd"))
							.parse((new StringBuilder()).append(year).append(
									"-").append(startmonths).append("-1")
									.toString())));
			String tmpDate = (new StringBuilder()).append(year).append("-")
					.append(endmonths).append("-1").toString();
			Calendar c = Calendar.getInstance();
			c.setTime((new SimpleDateFormat("yyyy-MM-dd")).parse(tmpDate));
			c.set(5, c.getActualMaximum(5));
			params.put("endTime", (new SimpleDateFormat("yyyy-MM-dd")).format(c
					.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
			params.put("endTime", (new StringBuilder()).append(year)
					.append("-").append(endmonths).append("-1").toString());
		}
		if (type != null && !type.equals("")) {
			params.put("lrtype", type);
			ILeavetypeBO lt_BO = (ILeavetypeBO) getBean("leavetypeBO");
			params.put("lrTypeShow", lt_BO.getLeavetype(type).getLtName());
		} else {
			params.put("lrtype", "all");
			params.put("lrTypeShow", "\u5168\u90E8\u7C7B\u578B");
		}
		if (deptInclude != null && !deptInclude.equals("")) {
			params.put("lrStatus", new Integer(1));
			params.put("lrStatusShow", "\u5305\u542B");
		} else {
			params.put("lrStatus", new Integer(0));
			params.put("lrStatusShow", "\u4E0D\u5305\u542B");
		}
		request.setAttribute("_params", params);
		request.setAttribute("_report",
				"/report/examin/Leaverequest_ByTime_Chartreport.rptdesign");
		return "success";
	}

	public String executeEmpOt() throws Exception {
		ServletRequest request = getRequest();
		if (getRequest().getParameter("_format") != null
				&& getRequest().getParameter("_format").equals("pdf")) {
			request.setAttribute("_format", "PDF");
			request.setAttribute("_contentType", "attachment");
			request.setAttribute("_fileName", "empOtReport");
		} else {
			request.setAttribute("_format", "HTML");
		}
		Map params = new HashMap();
		String year = getRequest().getParameter("year");
		String startmonths = getRequest().getParameter("startMonths")
				.substring(1);
		String endmonths = getRequest().getParameter("endMonths").substring(1);
		String type = getRequest().getParameter("examinType");
		String deptInclude = getRequest().getParameter("includeDeptPass");
		try {
			params.put("startTime", (new SimpleDateFormat("yyyy-MM-dd"))
					.format((new SimpleDateFormat("yyyy-MM-dd"))
							.parse((new StringBuilder()).append(year).append(
									"-").append(startmonths).append("-1")
									.toString())));
			String tmpDate = (new StringBuilder()).append(year).append("-")
					.append(endmonths).append("-1").toString();
			Calendar c = Calendar.getInstance();
			c.setTime((new SimpleDateFormat("yyyy-MM-dd")).parse(tmpDate));
			c.set(5, c.getActualMaximum(5));
			params.put("endTime", (new SimpleDateFormat("yyyy-MM-dd")).format(c
					.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
			params.put("endTime", (new StringBuilder()).append(year)
					.append("-").append(endmonths).append("-1").toString());
		}
		if (type != null && !type.equals("")) {
			params.put("lrtype", type);
			IOvertimetypeBo ot_BO = (IOvertimetypeBo) getBean("overtimetypeBO");
			params.put("lrTypeShow", ot_BO.searchByID(type).getOtName());
		} else {
			params.put("lrtype", "all");
			params.put("lrTypeShow", "\u5168\u90E8\u7C7B\u578B");
		}
		if (deptInclude != null && !deptInclude.equals("")) {
			params.put("lrStatus", new Integer(1));
			params.put("lrStatusShow", "\u5305\u542B");
		} else {
			params.put("lrStatus", new Integer(0));
			params.put("lrStatusShow", "\u4E0D\u5305\u542B");
		}
		request.setAttribute("_params", params);
		request.setAttribute("_report",
				"/report/examin/Overtime_ByTime_Chartreport.rptd.rptdesign");
		return "success";
	}

	public String executeEmpAly() throws Exception {
		ServletRequest request = getRequest();
		if (getRequest().getParameter("_format") != null
				&& getRequest().getParameter("_format").equals("pdf")) {
			request.setAttribute("_format", "PDF");
			request.setAttribute("_contentType", "attachment");
			request.setAttribute("_fileName", "empAttendMonthlyReport");
		} else {
			request.setAttribute("_format", "HTML");
		}
		Map params = new HashMap();
		String title = "";
		String reportTypeSelect = getRequest().getParameter("reportTypeSelect");
		String attendmonthlyYear = getRequest().getParameter(
				"attendmonthlyYear");
		String attendmonthlyMonths = getRequest().getParameter(
				"attendmonthlyMonths");
		if (reportTypeSelect.equals("1"))
			title = (new StringBuilder())
					.append(attendmonthlyYear)
					.append("\u5E74")
					.append(attendmonthlyMonths)
					.append("\u6708")
					.append(
							"\u8003\u52E4\u62A5\u8868\uFF08\u6309\u90E8\u95E8\uFF09-\u8BF7\u5047\uFF08\u5929\uFF09")
					.toString();
		else if (reportTypeSelect.equals("2"))
			title = (new StringBuilder())
					.append(attendmonthlyYear)
					.append("\u5E74")
					.append(attendmonthlyMonths)
					.append("\u6708")
					.append(
							"\u8003\u52E4\u62A5\u8868\uFF08\u6309\u90E8\u95E8\uFF09-\u52A0\u73ED\uFF08\u5C0F\u65F6\uFF09")
					.toString();
		else if (reportTypeSelect.equals("3"))
			title = (new StringBuilder())
					.append(attendmonthlyYear)
					.append("\u5E74")
					.append(attendmonthlyMonths)
					.append("\u6708")
					.append(
							"\u8003\u52E4\u62A5\u8868\uFF08\u6309\u90E8\u95E8\uFF09-\u8FDF\u5230\uFF08\u6B21\uFF09")
					.toString();
		else if (reportTypeSelect.equals("4"))
			title = (new StringBuilder())
					.append(attendmonthlyYear)
					.append("\u5E74")
					.append(attendmonthlyMonths)
					.append("\u6708")
					.append(
							"\u8003\u52E4\u62A5\u8868\uFF08\u6309\u90E8\u95E8\uFF09-\u65E9\u9000\uFF08\u6B21\uFF09")
					.toString();
		else if (reportTypeSelect.equals("5"))
			title = (new StringBuilder())
					.append(attendmonthlyYear)
					.append("\u5E74")
					.append(attendmonthlyMonths)
					.append("\u6708")
					.append(
							"\u8003\u52E4\u62A5\u8868\uFF08\u6309\u90E8\u95E8\uFF09-\u65F7\u5DE5\uFF08\u5929\uFF09")
					.toString();
		params.put("title", title);
		IExaminReportBo examinReportBo = (IExaminReportBo) SpringBeanFactory
				.getBean("examinReportBo");
		List attendMlyList = examinReportBo.getAlltendMlyObjects(
				reportTypeSelect, attendmonthlyYear, attendmonthlyMonths);
		params.put("attendMlyList", attendMlyList);
		request.setAttribute("_params", params);
		request.setAttribute("_report",
				"/report/examin/attendmonthly.rptdesign");
		return "success";
	}

	public List getLrtypeList() {
		return lrtypeList;
	}

	public void setLrtypeList(List lrtypeList) {
		this.lrtypeList = lrtypeList;
	}

	public List getOttypeList() {
		return ottypeList;
	}

	public void setOttypeList(List ottypeList) {
		this.ottypeList = ottypeList;
	}

	public List getYearList() {
		return yearList;
	}

	public void setYearList(List yearList) {
		this.yearList = yearList;
	}

	public String getReportSelect() {
		return reportSelect;
	}

	public void setReportSelect(String reportSelect) {
		this.reportSelect = reportSelect;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getStartMonths() {
		return startMonths;
	}

	public void setStartMonths(String startMonths) {
		this.startMonths = startMonths;
	}

	public String getEndMonths() {
		return endMonths;
	}

	public void setEndMonths(String endMonths) {
		this.endMonths = endMonths;
	}

	public String getDesignSelect() {
		return designSelect;
	}

	public void setDesignSelect(String designSelect) {
		this.designSelect = designSelect;
	}

	public String getExaminType() {
		return examinType;
	}

	public void setExaminType(String examinType) {
		this.examinType = examinType;
	}

	public String getIncludeDeptPass() {
		return includeDeptPass;
	}

	public void setIncludeDeptPass(String includeDeptPass) {
		this.includeDeptPass = includeDeptPass;
	}

	public String getReportFileLocation() {
		return reportFileLocation;
	}

	public void setReportFileLocation(String reportFileLocation) {
		this.reportFileLocation = reportFileLocation;
	}

	public String getAttendmonthlyYear() {
		return attendmonthlyYear;
	}

	public void setAttendmonthlyYear(String attendmonthlyYear) {
		this.attendmonthlyYear = attendmonthlyYear;
	}

	public String getAttendmonthlyMonths() {
		return attendmonthlyMonths;
	}

	public void setAttendmonthlyMonths(String attendmonthlyMonths) {
		this.attendmonthlyMonths = attendmonthlyMonths;
	}

	public List getAttendmonthlyYearList() {
		return attendmonthlyYearList;
	}

	public void setAttendmonthlyYearList(List attendmonthlyYearList) {
		this.attendmonthlyYearList = attendmonthlyYearList;
	}

	private static final long serialVersionUID = 0xf20d762a6f705610L;
	private List lrtypeList;
	private List ottypeList;
	private List yearList;
	private List attendmonthlyYearList;
	private String reportSelect;
	private String examinType;
	private String year;
	private String startMonths;
	private String endMonths;
	private String attendmonthlyYear;
	private String attendmonthlyMonths;
	private String includeDeptPass;
	private String designSelect;
	private String reportFileLocation;
}
